﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CommonLib;
using Constructor.Frames;
namespace Constructor
{
    public partial class FuncParam : UserControl
    {
        public ParameterInfo Param { get; set; }
        public Frame Frame { get; set; }
        public NewItem Item { get; set; }
        public Procedure Procedure { get; set; }
        public FuncParam(ParameterInfo p, Procedure pr)
        {
            InitializeComponent();
            Param = p;
            Frame = pr.Production.OwnerSlot.Frame;
            Item = pr.Production.Item;
            Procedure = pr;
            lbl_name.Text = p.Name;
            Init();
        }
        public void Init()
        {
            if (Param.ParameterType == typeof(int))
            {
                InitValueFromIntSlot(true, Frame, null);
                if (Procedure.Params[Param.Position].Text != "")
                    cmb_value.Text = Procedure.Params[Param.Position].Text;
            }
            else if (Param.ParameterType == typeof(Frame))
            {
                InitValueFromFrames(true, Frame, null);
                if (Procedure.Params[Param.Position].Text != "")
                    cmb_value.Text = Procedure.Params[Param.Position].Text;
            }
        }
        private void InitValueFromFrames(bool first, Frame frame, NewItem item)
        {
            if (first)
            {
                item = new NewItem(frame.ID, null);
                cmb_value.Items.Clear();
            }
            foreach (Slot s in frame.AllSlots)
                if (s.Type == SlotType.SubFrame)
                {
                    NewItem i = new NewItem(s);
                    i.AddRange(item);
                    i.Add(s.ID);
                    cmb_value.Items.Add(i);
                    InitValueFromFrames(false, s.Subframe, i);
                }
        }
        private void InitValueFromIntSlot(bool first, Frame frame, NewItem item)
        {
            if (first)
            {
                item = new NewItem(frame.ID, null);
                cmb_value.Items.Clear();
            }
            foreach (Slot s in frame.AllSlots)
                if (s.Type == SlotType.Regular && s.DomainID != null)
                {
                    if (s.Domain.Type == DomainType.Number)
                    {
                        NewItem i = new NewItem(s);
                        i.AddRange(item);
                        i.Add(s.ID);
                        cmb_value.Items.Add(i);
                    }
                }
                else
                    if (s.Type == SlotType.SubFrame)
                    {
                        NewItem i = new NewItem(s);
                        i.AddRange(item);
                        i.Add(s.ID);
                        InitValueFromIntSlot(false, s.Subframe, i);
                    }
        }

        private void cmb_value_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmb_value.SelectedItem as NewItem != null)
                Procedure.Params[Param.Position] = cmb_value.SelectedItem as NewItem;
        }
    }
}
